<?php
namespace app\common\service;


class NoticeService extends BaseService
{


    /**
     * 新增或编辑公告
     * @param int $notice_id
     * @param array $postParam
     * @return int|string
     * @throws \think\Exception
     */
    public function saveNotice($sid = 0, $notice_id = 0, $postParam = []){
        $data = [
            'sid' => $sid,
            'notice_title' => $postParam['notice_title'],
            'notice_content' => htmlspecialchars($postParam['notice_content']),
            'notice_author' => $postParam['notice_author'] ?? '',
            'notice_keyword' => $postParam['notice_keyword'] ?? '',
            'notice_sort' => $postParam['notice_sort'],
            'notice_status' => $postParam['notice_status'],
            'edit_time' => time(),
            'send_obj' => $postParam['send_obj'] ?? 1,
            'share_title' => $postParam['share_title'] ?? '',
            'share_img' => $postParam['share_img'] ?? '',
            'share_description' => $postParam['share_description'] ? htmlspecialchars($postParam['share_description']) :  '',
        ];

        if($notice_id){
            $result = db('notice')->where(['notice_id' => $notice_id])->update($data);
        } else {
            $result = db('notice')->insert($data);
        }

        return $result;
    }

    /**
     * 获取公告详情
     * @param $notice_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getDetail($notice_id){
        $info = db('notice')->where(['notice_id' => $notice_id, 'notice_status' => ['<>', -1]])->find();
        $info['edit_time'] = date('Y-m-d H:i:s', $info['edit_time']);
        $info['notice_content'] = htmlspecialchars_decode(addslashes($info['notice_content']));
        $info['share_description'] = $info['share_description'] ? htmlspecialchars_decode(addslashes($info['share_description'])) : '';
        return $info;
    }

    /**
     * 软删除公告
     * @param $notice_id
     * @return int
     */
    public function deleteNotice($notice_id){
        return db('notice')->where(['notice_id' => $notice_id])->setField('notice_status', -1);
    }


    /**
     * 获取公告列表
     * @param int $p
     * @param int $pageSize
     * @return array
     */
    public function getNoticeList($send_obj = 1,$p = 1, $pageSize = 10){
        $where = [
            'notice_status' => ['<>', -1],
            'send_obj' => $send_obj
        ];
        $count = db('notice')
                ->where($where)
                ->count();
        $list = db('notice')
                ->where($where)
                ->order('edit_time DESC')
                ->limit(($p-1)*$pageSize, $pageSize)
                ->select();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['edit_time'] = date('Y-m-d H:i:s', $v['edit_time']);
                $list[$k]['notice_content'] = htmlspecialchars_decode(str_replace('amp;','',$v['notice_content']));
            }
        }
        $data = [
            'page' => ['count' => $count],
            'content' => $list
        ];
        return $data;
    }


    /**
     * 获取未读公告
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getUnreadNotice($sid){
        $where = [
            'notice_status' => 1,
            'sid' => 0
        ];
        //查询最新一条公告
        $info = db('notice')->where($where)->order('notice_id DESC')->find();
        if($info) {

            //查询是否在已阅读表中记录
            $read_info = db('notice_read')->where(['nid' => $info['notice_id'], 'sid' => $sid, 'is_read' => 1])->find();
            if ($read_info) {
                return false;
            }

            $info['edit_time'] = date('Y-m-d H:i:s', $info['edit_time']);
            $info['notice_content'] = htmlspecialchars_decode(str_replace('amp;','',$info['notice_content'])) ?? '';
            return $info;
        }
        return false;
    }


    /**
     * 确认阅读公告
     * @param $sid
     * @param $notice_id
     * @return bool|int|string
     * @throws \think\Exception
     */
    public function confirmReadNotice($sid, $notice_id){
        //查询系统公告
        $sys_list = db('notice')->field('notice_id')->where(['notice_status' => 1, 'sid' => 0])->select();
        $sys_notice_ids = [];
        foreach($sys_list as $key => $val){
            $sys_notice_ids[$key] = $val['notice_id'];
        }

        //查询出该店铺已读公告
        $read_list = db('notice_read')->field('nid')->where(['nid' => ['in',$sys_notice_ids], 'uid' => 0, 'sid' => $sid, 'is_read' => 1])->select();
        $read_notice_ids = [];
        foreach($read_list as $ky => $vl){
            $read_notice_ids[$ky] = $vl['nid'];
        }

        //差集
        $diff_ids = array_diff($sys_notice_ids, $read_notice_ids);
        $data = [];
        if($diff_ids){
            foreach($diff_ids as $k => $v){
                $data[$k]['nid'] = $v;
                $data[$k]['sid'] = $sid;
                $data[$k]['read_num'] = 1;
                $data[$k]['is_read'] = 1;
            }
            return db('notice_read')->insertAll($data);
        } else {
            return true;
        }

        /*//查询出所有未读公告
        $field = 'n.notice_id,nr.read_id';
        $unread_list = db('notice n')
                        ->field($field)
                        ->join('notice_read nr', 'nr.nid = n.notice_id', 'LEFT')
                        ->where(['n.notice_status' => 1, 'n.sid' => 0])
                        ->select();
        dump($unread_list);die();

        $data = [];
        if($unread_list){
            foreach($unread_list as $k => $v){
                if(empty($v['read_id'])){
                    $data[$k]['nid'] = $v['notice_id'];
                    $data[$k]['sid'] = $sid;
                    $data[$k]['read_num'] = 1;
                    $data[$k]['is_read'] = 1;
                } else {
                    db('notice_read')->where(['nid' => $v['notice_id'], 'sid' => $sid])->setInc('read_num', 1);
                }
            }
        }
        $result = true;
        if($data){
            $result = db('notice_read')->insertAll($data);
        }
        return $result;*/
    }

}






















